package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.dto.YddUserPageDTO;
import com.youdianda.entity.YddUser;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.YddUserService;
import com.youdianda.mapper.YddUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xliu
 * @description 针对表【ydd_user】的数据库操作Service实现
 * @createDate 2023-05-14 16:01:34
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser>
        implements YddUserService {

    @Autowired
    private YddUserMapper yddUserMapper;

    /**
     * 用户列表
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageResult> list(YddUserPageDTO dto) {
        //查询用户信息
        String userName = dto.getUsername();
        String mobile = dto.getMobile();
        String sex = dto.getSex();
        LambdaQueryWrapper<YddUser> lqw = new LambdaQueryWrapper<>();
        lqw.like(userName != null, YddUser::getUsername, userName)
                .like(mobile != null, YddUser::getMobile, mobile)
                .like(sex != null, YddUser::getSex, sex);

        //开启分页
        IPage<YddUser> ipage = new Page<>(Long.valueOf(dto.getPage()), Long.valueOf(dto.getLimit()));
        IPage<YddUser> userIPage = yddUserMapper.selectPage(ipage, lqw);

        //封装数据并返回
        PageResult result = new PageResult();
        result.setCount((int) userIPage.getTotal());        //总数
        result.setCurrentPage((int) userIPage.getCurrent());//当前页数
        result.setPageSize((int) userIPage.getSize());      //每页数量
        result.setTotalPages((int) userIPage.getPages());   //总页数
        result.setData(ipage.getRecords());
        return Result.success(result);
    }

    /**
     * 用户修改
     *
     * @param yddUser
     * @return
     */
    @Override
    public Result updateUser(YddUser yddUser) {
        //非空判断
        if (yddUser.getId() == null) {
            return Result.error("id为空");
        }
        //修改信息
        YddUser updateUser = new YddUser();
        BeanUtils.copyProperties(yddUser, updateUser);
        yddUserMapper.updateById(updateUser);
        return Result.success();
    }
}




